Component({
  properties: {
    filterList: {
      type: Array,
      value: []
    },
    initForm: {
      type: Object,
      value: null,
    }
  },
  observers: {
    initForm: function (initForm) {
      if (initForm) {
        this.setData({form: initForm})
      }
    }
  },
  data: {
    filterList: [],
    form: {},
    curForm: null,
    curIndex: null,
    curFilter: {},
    clickIndex: null
  },
  methods: {
    onLoad() {
      console.log(this.data.filterList)
      
    },
    handleShow(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({clickIndex: index})
      if (this.data.curIndex === index) {
        this.setData({curIndex: null});
        this.emitStatus(0)
        return;
      }
      let curFilter = this.data.filterList[index]
      let curForm = this.data.form[curFilter.id];
      if (curForm === undefined) {
        curForm = null;
      }
      this.setData({curIndex: index, curForm: curForm, curFilter: curFilter})
      this.emitStatus(1)
    },

    handleSelect(e) {
      let item = e.currentTarget.dataset.item;
      let form = this.data.form;
      let curFilter = this.data.curFilter

      if (curFilter.type === 'single') {
        form[curFilter.id] = {
          value: item.id,
          title: item.subTitle? item.subTitle:item.title,
          noActive: item.noActive
        };
        if (!item.noActive && curFilter.exclusion && curFilter.exclusion.length > 0) {
          for(let i = 0; i < curFilter.exclusion.length; i++) {
            delete form[curFilter.exclusion[i]]
          }
        }
        this.setData({form: form, curIndex: null});
        console.log(this.data)
        this.triggerEvent('getStoreList', [this.data.clickIndex,item])
        this.emitChange();
        return;
      }

      let children = item.children
      // 没有子菜单
      if (!children || children.length === 0) {
        form[curFilter.id] = {
          value: [item.id],
          title: item.title
        };
        this.setData({form: form, curIndex: null});
        this.emitChange();
        return;
      }
      // 继续二级菜单选择
      let curForm = {
        value: [item.id],
        title: item.title
      }
      this.setData({curForm})
    },

    handleSelectSecond(e) {
      let item = e.currentTarget.dataset.item;
      let curForm = this.data.curForm;
      let form = this.data.form;
      form[this.data.curFilter.id] = {
        value: [curForm.value[0], item.id],
        title: item.subTitle? item.subTitle: item.title,
        noActive: item.noActive
      };
      this.setData({form: form, curForm: curForm, curIndex: null});
      this.emitChange();
    },
    setForm(form) {
      this.setData({form: form});
    },
    close() {
      this.setData({
        curIndex: null
      })
      this.triggerEvent('status', 0)
    },
    emitChange() {
      this.triggerEvent('status', 0)
      this.triggerEvent('change', this.data.form)
    },
    emitStatus(status) {
      this.triggerEvent('status', status)
    }
  }
});
